//
//  EmaOverseaSdkApi.cpp
//  clientOversea
//
//  Created by ming on 2019/1/9.
//

#include "EmaOverseaSdkApi.hpp"
#import "ChannelAPI_Oversea.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
//std::string getVersionNum(){
//    return "";
//}

std::string EmaOverseaSdkApi::getVersion(){
    NSString * resV = [ChannelAPI getVersion];
    return [resV UTF8String];
}

std::string EmaOverseaSdkApi::getResourceVersion()
{
    NSString * resV = [ChannelAPI getResourceVersion];
    return [resV UTF8String];
}

std::string EmaOverseaSdkApi::getCdn(){
    NSString * resV = [ChannelAPI getCdn];
    return [resV UTF8String];
}

bool EmaOverseaSdkApi::isInitialized(){
    int ret = [ChannelAPI getInitState];
    return ret == 0;
}

void EmaOverseaSdkApi::jumpToAppShop(){
   [ChannelAPI jumpToAppShop];
}

bool EmaOverseaSdkApi::isReview(){
    return (bool)[ChannelAPI isReview];
}

void EmaOverseaSdkApi::initSdk(){
    [ChannelAPI init];
}

int EmaOverseaSdkApi::getInitSdkState(){
    return [ChannelAPI getInitState];
}
bool EmaOverseaSdkApi::checkPermissions(){
    return true;
}
void EmaOverseaSdkApi::requestPermissions(){
//    cocos2d::EventCustom customEvent("CHANNEL_REQUEST_PREMISSIONS_EVENT");
//    std::string param_ = "success";
//    customEvent.setUserStr(param_);
//    cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&customEvent);
}

void EmaOverseaSdkApi::goAppSetting(){
    
}

void EmaOverseaSdkApi::activeBuriedPointReport(const char* data){
    if(data != nullptr){
        NSDictionary * dict = @{@"jsonStr":[NSString stringWithUTF8String:data]};
        [ChannelAPI activeBuriedPointReport:dict];
    }
   
}
// --- eventName 事件名称 active AdJust识别码 result 结果
void EmaOverseaSdkApi::reportActiveBuriedPoint(const char * trigger, int result, int duration){
    rapidjson::StringBuffer s;
    rapidjson::Writer<rapidjson::StringBuffer> writer(s);
    writer.StartObject();
    writer.Key("event");
    writer.String("dc_activation_log");
    writer.Key("channels");
    writer.StartArray();
    writer.String("AppsFlyer");
    writer.String("Adjust");
    writer.EndArray();
    writer.Key("params");
    writer.StartObject();
    writer.Key("result");
    writer.Int(result);
    writer.Key("dc_activation_log");
    writer.String("el5uny");
    writer.Key("trigger");
    writer.String(trigger);
    if(duration != 0){
        writer.Key("duration");
        writer.Int(duration);
    }
    writer.EndObject();
    writer.EndObject();
    std::string jsonStr = s.GetString();
    activeBuriedPointReport(jsonStr.c_str());
}
